import React, { useEffect, useState } from 'react'

export default function App() {
  let [opacity, setOpacity] = useState(1)
  useEffect(() => {
    const opacityTimer = setInterval(() => {
      opacity -= 0.1
      if (opacity <= 0) {
        opacity = 1
      }
      setOpacity(opacity)

      return () => {
        clearInterval(opacityTimer)
      }
    }, 100)

  }
  //只需要在初始化的时候生成一个定时器,在重新加载页面的时候关闭定时器
  //所以在这个位置不需要加参数
    , [])

  return (
    <div>
      <h1 style={{ opacity }}>有花堪折直须折,莫待无花空折枝</h1>
    </div>
  )
}
